//
//  HomeViewController.m
//  XPhoto
//
//  Created by baboy on 2019/2/20.
//  Copyright © 2019 baboy. All rights reserved.
//

#import "HomeViewController.h"
#import "Post.h"
#import "BKIndicator.h"
#import "PhotoTableViewCell.h"
#import "PostDetailViewController.h"
#import "PostAction.h"
#import "SerialPostDetailViewController.h"
#import "BKCommentView.h"
#import "Comment.h"
#import "LoginViewController.h"
#import "BKNavigationController.h"
#import "BKWebViewController.h"
#import <UShareUI/UShareUI.h>
#import "UIViewController+share.h"
#import "ShareUtil.h"
#import "UIScrollView+x.h"
#import "ExploreViewController.h"
#import "UIViewController+camera.h"

@interface HomeViewController ()<PhotoTableViewCellDelegate>
@property(nonatomic, strong) User *user;
@end

@implementation HomeViewController
- (void)dealloc{
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}
- (id)init{
    if (self = [super initWithNibName:NSStringFromClass([self class]) bundle:nil]){
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(postPublished:) name:NOTIFICATION_POST_PUBLISHED object:nil];
        self.posts = [NSMutableArray array];
    }
    return self;
}
- (void)viewDidLoad {
    [super viewDidLoad];
//    self.tableView.tableFooterView = [UIView new];
    
    
//    self.tableView.contentInsetAdjustmentBehavior = NO;
    self.user = [User currentUser];
    self.tableView.tableFooterView = [UIView new];
    self.tableView.refresh = YES;
    
    [self searchWithOffset:0];
    
//    LoginViewController *vc = [[LoginViewController alloc] init];
//    BKWebViewController *vc = [[BKWebViewController alloc] init];
//    BKNavigationController *nav = [[BKNavigationController alloc] initWithRootViewController:vc];
//    [self presentViewController:nav animated:YES completion:nil];
    
    // Do any additional setup after loading the view from its nib.
//    [ShareUtil loginFromPlatform:UMSocialPlatformType_Sina callback:nil];
    
    
}

- (void)searchWithOffset:(NSInteger)offset{
    PostSearchArgs *args = [PostSearchArgs new];
    args.followerUid = @(self.user.uid);
    args.offset = offset;
    [Post searchWithArgs:args
                callback:^(id  task, id  photos, NSError * _Nonnull error) {
        if (error) {
            [BKIndicator showMessage:error.localizedDescription duration:3.0f completion:nil];
        }
        if (!error) {
            if (offset == 0) {
                self.posts = [NSMutableArray array];
            }
            [self.posts addObjectsFromArray:photos];
            [self.tableView reloadData];
        }
        self.maskView.hidden = self.posts.count > 0;
        [self.tableView stopRefreshing];
        [self.tableView stopLoading];
        self.tableView.loadMore = [photos count]%10==0;
    }];
}
-(void)postPublished:(NSNotification *)noti{
    Post *post = noti.userInfo[@"post"];
    if (post) {
        [self.posts addObject:post];
        [self.tableView reloadData];
    }
}
#pragma BKScrollViewDragRefreshDelegate
- (void)scrollViewWillBeginRefreshing:(BKTableView *)tableView{
    [self searchWithOffset:0];
}
- (void)scrollViewWillBeginLoadingMore:(BKTableView *)tableView{
    [self searchWithOffset:self.posts.count];
}
#pragma mark - UIScrollViewDelegate
-(void)scrollViewDidScroll:(UIScrollView *)scrollView{
    [self.tableView scrollViewDidScroll];
}
-(void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate{
    [self.tableView scrollViewDidEndDragging];
}
#pragma mark - action

- (IBAction)takePhoto:(id)sender{
    [super takePhoto];
}
- (IBAction)explore:(id)sender{
    self.tabBarController.selectedIndex = 1;
}
/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end

